package com.deyuanyun.pic.pipe.controller;

import com.caixu.beanvalidateutil.imlclass.BeanValidateUtil;
import com.caixu.beanvalidateutil.imlclass.VResult;
import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.tool.StaticParam;
import com.deyuanyun.pic.common.util.MyCollectionUtil;
import com.deyuanyun.pic.common.util.RequestUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxResponse;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.poi.ReadExcel;
import com.deyuanyun.pic.pipe.domain.exceldomain.PipeLineCentreImportDemo;
import com.deyuanyun.pic.pipe.service.PipeLineService;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * 管道和管道中心线数据初始化
 * 
 * @author scalpel
 * @version 1.0
 * @date 2016年1月26日上午9:56:30
 * Method List:                    
 *             1.---------------- 
 *             
 *             importData 导入管道中心线数据
 *             importBase 导入管道基本信息
 *             del        删除管道数据
 *             gisdel     删除管道中心线数据
 *             search     查询数据
 *History:                          
 */
@Controller
@RequestMapping("/pipe/line")
public class PipeLineController extends PipeBaseController{

	@Resource
	private PipeLineService lineService;
//	
//	@RequestMapping("/page.html")
//	public ModelAndView page(HttpServletRequest request){
//		ModelAndView mdv=new ModelAndView();
//		
//		
//		return mdv;
//	}
//	
	@RequestMapping("/search.json")
	@ResponseBody
	public void search(HttpServletRequest request){
		PageJsonBean jsonBean=new PageJsonBean();
		HashMap map=RequestUtil.parseRequestParamToHashMap(request);
		Page main=lineService.findPage(map);
		jsonBean.setList(main);
		AjaxSupport.sendSuccessText("ok", jsonBean);
		
	}
	
	
	
	@RequestMapping("/import.json")
	@ResponseBody
	public void importData(@RequestParam MultipartFile file,HttpServletResponse response) throws IOException, Exception{
		List<PipeLineCentreImportDemo> data= ReadExcel.readExcle(PipeLineCentreImportDemo.class,file.getInputStream(),5);
		data= MyCollectionUtil.removeLastEmpty(data);
		VResult b= BeanValidateUtil.vali2(data);
		if (b.isRelult()){
			lineService.importData(data);
			 AjaxResponse.sendText(response, StaticParam.UPLOAD_FILE_SUCCESS);
		}else{
			String m=b.getAllString();
            response.setStatus(503);
            AjaxResponse.sendText(response,StaticParam.getUploadFileFailMessage(m));
		}
		
	}
	
	@RequestMapping("/importBase.json")
	@ResponseBody
	public void importBase(@RequestParam MultipartFile file,HttpServletResponse response) throws IOException, Exception{
		List<PipeLineCentreImportDemo> data= ReadExcel.readExcle(PipeLineCentreImportDemo.class,file.getInputStream(),5);
		data= MyCollectionUtil.removeLastEmpty(data);
		VResult b= BeanValidateUtil.vali2(data);
		if (b.isRelult()){
			lineService.importData(data);
			 AjaxResponse.sendText(response,StaticParam.UPLOAD_FILE_SUCCESS);
		}else{
			String m=b.getAllString();
            response.setStatus(503);
            AjaxResponse.sendText(response,StaticParam.getUploadFileFailMessage(m));
		}
		
	}
	
	@RequestMapping("/del.json")
	@ResponseBody
	public void del(HttpServletRequest request){
		String id=request.getParameter("id");
		if(!StringUtils.isEmpty(id))
		{
			AjaxSupport.sendSuccessText("ok", "{\"statue\":0}");
		}else{
			AjaxSupport.sendSuccessText("ok", "{\"statue\":1}");
		}
		
	
	}
	
	@RequestMapping("/gisdel.json")
	@ResponseBody
	public void gisdel(HttpServletRequest request){
		HashMap data=RequestUtil.parseRequestParamToHashMap(request);
		int i=lineService.delGis(data);
		if(i ==1){
			AjaxSupport.sendSuccessText("ok", "{\"statue\":0}");
		}else{
			AjaxSupport.sendSuccessText("ok", "{\"statue\":1}");
		}
	}
}
